package com.yuandengta.yomall.order.config;

import com.yuandengta.yomall.order.constant.RabbitConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Rabbit 消息队列配置 {@link RabbitConfig}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@Configuration
public class RabbitConfig {

    /**
     * 使用JSON序列化机制进行消息转换
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange(RabbitConstant.ORDER_EVENT_EXCHANGE, true, false);
    }

    /**
     * 延时队列
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", RabbitConstant.ORDER_EVENT_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", RabbitConstant.ORDER_DELAY_DEAD_LETTER_ROUTING_KEY);
        // 单位：毫秒
        arguments.put("x-message-ttl", 60000);

        return new Queue(RabbitConstant.ORDER_DELAY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Queue orderReleaseQueue(){
        return new Queue(RabbitConstant.ORDER_RELEASE_ORDER_QUEUE, true, false, false);
    }

    @Bean
    public Binding orderCreateBinding(){
        return new Binding(RabbitConstant.ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE, RabbitConstant.ORDER_DELAY_QUEUE_ROUTING_KEY, null);
    }

    @Bean
    public Binding orderReleaseBinding(){
        return new Binding(RabbitConstant.ORDER_RELEASE_ORDER_QUEUE, Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE, RabbitConstant.ORDER_RELEASE_QUEUE_ROUTING_KEY, null);
    }

    /**
     * 订单释放直接和库存释放进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding(RabbitConstant.STOCK_RELEASE_QUEUE, Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE, RabbitConstant.STOCK_RELEASE_QUEUE_ROUTING_KEY, null);
    }

    /**
     * 秒杀订单队列
     * @return
     */
    @Bean
    public Queue orderSeckillQueue(){
        return new Queue(RabbitConstant.ORDER_SECKILL_ORDER_QUEUE, true, false, false);
    }

    @Bean
    public Binding orderSeckillBinding(){
        return new Binding(RabbitConstant.ORDER_SECKILL_ORDER_QUEUE, Binding.DestinationType.QUEUE,
                RabbitConstant.ORDER_EVENT_EXCHANGE, RabbitConstant.ORDER_SECKILL_ORDER_QUEUE_ROUTING_KEY, null);
    }

}
